/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.bomt.utils;

import com.comac.ins.bomt.domain.DetailBomBase;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * Bom核心工具类
 *
 * @author zhanghongyi
 * @date 2024 /8/29
 * @description BomCoreUtil
 */
public class BomCoreUtil {
    /**
     * Group by level code map.
     *
     * @param <T>     the type parameter
     * @param details the details
     * @return the map
     */
    public static <T extends DetailBomBase> Map<String, List<T>> groupByLevelCode(List<T> details) {
        Map<String, List<T>> result = new LinkedHashMap<>();
        List<T> currentGroup = new ArrayList<>();
        String currentGroupCode = null;
        for (T detail : details) {
            if (StringUtils.equals("1", detail.getLevel())) {
                if (StringUtils.isNotBlank(currentGroupCode)) {
                    result.put(currentGroupCode, currentGroup);
                    currentGroup = new ArrayList<>();
                }
                String codeCut = StringUtils.split(detail.getCode(), ".")[0];
                currentGroupCode = codeCut.substring(0, codeCut.length() - 2);
            }
            currentGroup.add(detail);
        }
        result.putIfAbsent(currentGroupCode, currentGroup);
        return result;
    }
}
